package edu.msse.signup.service;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import edu.msse.signup.domain.Position;
import edu.msse.signup.domain.TimeSlot;
import edu.msse.signup.domain.Users;

public class TimeSlotService extends JpaDaoSupport{
	 
	
	static public TimeSlot createTimeSlot(Date startTime, Date endTime, Position position, int numPositions)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            if(position!=null) 
        	{
            	position=jpa.merge(position);
        	}
            
            TimeSlot t=new TimeSlot(startTime, endTime, position, numPositions);
    		jpa.persist(t);
            tx.commit();
            t=jpa.merge(t);
            return t;
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
        }
        
	}


	public Users addUser(TimeSlot timeSlot, Users user)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            timeSlot=jpa.merge(timeSlot);
            if(timeSlot.getUsers().size() < timeSlot.getNumberOfVolunteers()){
            	 user=jpa.merge(user);
                 user.addTimeSlot(timeSlot);
                 timeSlot=jpa.merge(timeSlot);
                 timeSlot.addUser(user);
                 user=jpa.merge(user);
                 jpa.persist(timeSlot);
                 jpa.persist(user); 	
            }
            else
            	throw new Exception("Timeslot Already Filled");
            
            tx.commit();
            return user;
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
        }
    }
	
	public static TimeSlot update(TimeSlot timeSlot)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            timeSlot=jpa.merge(timeSlot);
            jpa.persist(timeSlot);
            tx.commit();
            
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
            
        }
		return timeSlot;
	}
	
	public TimeSlot getTimeSlot(long id){
		return getJpaTemplate().find(TimeSlot.class, id);
	}
	
	public List getUsers(TimeSlot timeSlot){
		return getJpaTemplate().find("from Users u where ? in elements(u.timeSlots)", timeSlot);
	}
	
	public Boolean delete(TimeSlot timeSlot)
	{
		final TimeSlot aTimeSlot = timeSlot;
		return (Boolean) getJpaTemplate().execute(new JpaCallback() {
    		
			public Object doInJpa(EntityManager em) throws PersistenceException {
				EntityTransaction tx = em.getTransaction();
		        try {
		        	tx.begin();
		        	TimeSlot timeSlotMerged=em.merge(aTimeSlot);
		        	em.remove(timeSlotMerged);
		            tx.commit();
		            return true;
		            
		        } catch (Exception x) {
		        	x.printStackTrace();
		        	tx.rollback();
		        	return false;
		            
		        }
			}});

	}
}
